package test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import DBLayer.DBConnection;

public class DBDiagnose {
	public static void main(String[] args) throws SQLException {
		DBConnection dbc = DBConnection.getInstance();
		Connection con = dbc.getDBcon();
		Statement order = con.createStatement();
		ResultSet orderRS = order.executeQuery("select * from salesorder");
		
		Statement partOrder = con.createStatement();
		ResultSet partOrderRS = partOrder.executeQuery("select * from partorder");
		
		Statement product = con.createStatement();
		ResultSet productRS = product.executeQuery("select * from product");
		
		print("SalesOrder", orderRS);
		print("PartOrder", partOrderRS);
		print("Product", productRS);
	}
	
	private static void print(String tableName, ResultSet rs) throws SQLException {
		System.out.println(tableName + ": \n");
		ResultSetMetaData meta = rs.getMetaData();		
		int colCount = meta.getColumnCount();
		ArrayList<String> colNames = new ArrayList<>();
		for(int i = 0; i < colCount; i++) {
			colNames.add(meta.getColumnName(i+1));
			field(meta.getColumnName(i+1));
		}
		System.out.println("\n------------------------------------------------------------");
		
		while(rs.next()) {
			for(int i = 0; i < colNames.size(); i++) {
				field(rs.getObject(colNames.get(i)));	
			}			
			System.out.println();
		}
		System.out.println("------------------------------------------------------------\n");
		
	}
	
	private static void field(Object o) {
		String s = "";
		if (o != null){
			s = o.toString();
			if(s.length() > 7)
				s = s.substring(0, 7);
			System.out.print(s + "\t");
		}
		
	}

}
